<template>
  <div class="layout-container" style="padding: 0 !important;">
    <div class="layout-section layout-section2">

      <apply-notice :user-type="8"></apply-notice>

      <div class="com-contain">
        <div class="com-para">
          请最后核对以下信息：当前账号：<strong>{{ user.userName }}</strong>，企业名称：<strong>{{ info.unitName }}</strong>。
        </div>
      </div>
      <div class="com-contain" style="text-align: center; padding: 20px 0;">
        <van-button type="info" style="width: 90%;" :disabled="!isLegal" @click="realAuth">开始人脸识别</van-button>
        <br/>
        <div style="color: #ff0000;">{{ legalMsg }}</div>
      </div>

      <footer-right></footer-right>
    </div>
  </div>
</template>

<script>
import FooterRight from "@/components/footerRight";
import applyNotice from "@/components/applyNotice";
import {apiQrcodeLogin, apiSavePcAuthId} from "@/api/apply";
import {ajaxUtils, strUtils} from "@/utils/lwUtils";

export default {
  components: {FooterRight, applyNotice},
  data() {
    return {
      unid: "",
      info: {
        unid: "", unitName: "", mobile: "", unitCerno: "", unitLerep: "", createUserid: "", unitCartid: "",
      },
      user: {
        userId: "", userType: "", loginName: "", userName: "", idtype: "", idNum: "", legName: "",
      },
      errMsg: "",
    }
  },
  created() {
    this.unid = this.$route.query.unid;
    apiQrcodeLogin(this.unid).then(res => {
      console.log(res);
      if (ajaxUtils.isFail(res.code, res.msg)) {
        this.errMsg = "无法执行人脸识别操作：" + res.msg;
        return;
      }
      this.user = res.data.user;
      this.info = res.data.info;
    }).catch((err) => {
      console.log("-------------------------");
      console.log(err);
      console.log("-------------------------");
      this.errMsg = "无法执行人脸识别操作：数据初始化异常";
    });
  },
  methods: {
    //执行人脸识别操作
    realAuth: function () {
      if (strUtils.isEmpty(this.info.unitLerep) || strUtils.isEmpty(this.info.unitCerno)) {
        this.$eraqDlg.warning("信息不完整，请提交材料申请查询。");
        return;
      }
      ZWJSBridge.onReady(() => {
        ZWJSBridge.zmAuthentication({
          certNo: this.info.unitCerno, certName: this.info.unitLerep
          //certNo: "420821197912034314", certName: "何亮华"
        }).then((data) => {
          if (!data.pass) {
            this.$eraqDlg.alert("未通过人脸识别校验");
            return;
          }
          this.doSaveAuth(this.unid, data.passId);
        }).catch(error => {
          this.$eraqDlg.alert(strUtils.isEmpty(error) ? "调用人脸识别服务错误" : error);
        });
      });
    },
    //保存人脸识别校验结果
    doSaveAuth: function (unid, authId) {
      if (unid === "" || authId === "") {
        this.$eraqDlg.warning("未正确接收到人脸识别校验码");
        return;
      }
      apiSavePcAuthId(unid, authId).then(res => {
        if (ajaxUtils.checkFail(res.code, res.msg)) {
          return;
        }
        this.errMsg = "人脸识别操作成功";
        this.$eraqDlg.success("人脸识别操作成功");
        //this.legalMsg = "人脸识别操作成功！";
        //this.$router.push({path: "/list"});
      }).catch(() => {
        this.errMsg = "校验信息保存异常！";
      });
    },
  },
  computed: {
    isLegal: function () {
      return !strUtils.isEmpty(this.info.unitLerep) && !strUtils.isEmpty(this.info.unitCerno);
    },
    legalMsg: function () {
      if (this.errMsg !== "") {
        return this.errMsg;
      }
      return !strUtils.isEmpty(this.info.unitLerep) && !strUtils.isEmpty(this.info.unitCerno) ?
          "请等待成功提示后再关闭窗口。" : "信息不完整，无法执行人脸识别操作";
    }
  }
}
</script>

<style lang='scss' scoped>
@import "src/css/home";
@import "src/css/jzy";
@import "src/css/detail";

strong {
  color: #ff0000;
}

.com-para {
  line-height: 30px;
}
</style>
